package development;

import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.component.BottomBar;
import state.component.Event;
import state.component.EventListener;
import state.component.GameMenu;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import development.bean.Service;
import development.group.FrmGroupMenu;
import development.group.FrmGroupNapXuAndExchangeGold;
import development.socket.GameActionHandle;
import development.socket.SocketClientUtil;

public class FrmGameServiceList extends GameForm implements EventListener {
  private final int DISTANCE = 21;
  private final int DX = 10;
  private final int DY = 48;

  private GameImage muitenImage;
  private GameImage muitenFocusImage;

  private ImageText text8;
  private int chooseIndex;

  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmGameServiceList();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    GameActionHandle.isLoginSuccess = false;
    muitenImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "muiten.png");
    muitenFocusImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "muiten_focus.png");
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    
    // Create bottom bar
    GameMenu menu = new GameMenu(null, new String[] { "Update danh sách", "Nạp xu", "Tặng game", "Thông tin TK" }, 4, this, Constants.MENU_STYLE, 0, GameConstants.SCREEN_HEIGHT - 45);
    BottomBar bottomBar = DrawUtils.getBottomBar(_manager);
    bottomBar.setLabels("Menu", "Chọn", "Trở về");
    bottomBar.setLeftMenu("Menu", menu);
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    DrawUtils.drawTopBar(g);
    
    text8.drawString2(g, "Danh sách server", GameColor.WHITE_CODE, Constants.SCREEN_WIDTH / 2, 2, Constants.TOP_HCENTER_ANCHOR);

    // Draw game service list
    for (int i = 0; i < Global.gameServices.size(); i++) {
      g.setColor(GameColor.WHITE_CODE);
      g.drawLine(DX, DY - DISTANCE / 2 + i * DISTANCE, GameConstants.SCREEN_WIDTH - DX, DY - DISTANCE / 2 + i * DISTANCE);

      Service gameService = (Service) Global.gameServices.elementAt(i);
      String gameServiceName = gameService.getName() + " (" + gameService.getCocurrentUser() + "/" + gameService.getMaxUser() + ")";
      int color = GameColor.WHITE_CODE;
      if (i == chooseIndex) {
        g.setColor(0xCC999A);
        g.fillRect(DX, DY - DISTANCE / 2 + i * DISTANCE + 2, GameConstants.SCREEN_WIDTH - 2 * DX, DISTANCE - 3);
        color = GameColor.BLACK_CODE;
        g.drawImage(muitenFocusImage, DX + 4, DY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
      } else {
        g.drawImage(muitenImage, DX + 4, DY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
      }
      text8.drawString2(g, gameServiceName, color, 26, DY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
    }
  }

  public void onActionPerform(Event event) {
    // "Cập nhật danh sách", "Nạp xu", "Tặng game", "Thông tin TK"
    String action = event.getAction();
    if (action.equals("Update danh sách")) {
      SocketClientUtil.gameServiceListRequest();
    } else if (action.equals("Nạp xu")) {
      FrmGroupNapXuAndExchangeGold.backForm = this;
      Global.nextState(FrmGroupNapXuAndExchangeGold.instance(), null);
    } else if (action.equals("Tặng game")) {
      // TODO: FrmSendGame 
//      Global.nextState(FrmSendGame.instance(), null);
    } else if (action.equals("Thông tin TK")) {
      FrmUserInfo.goBackForm = this;
      Global.nextState(FrmUserInfo.instance(), null);
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.UP:
    case Key.K_2:
      chooseIndex--;
      if (chooseIndex < 0) {
        chooseIndex = Global.gameServices.size() - 1;
      }
      break;
    case Key.DOWN:
    case Key.K_8:
      chooseIndex++;
      if (chooseIndex > Global.gameServices.size() - 1) {
        chooseIndex = 0;
      }
      break;
    case Key.FIRE:
    case Key.K_5:
      if (Global.gameServices.size() > 0) {
        if (!Constants.GAME_VERSION_INFO.equals("all")) {
          Global.nextState(FrmListRoom.instance(), null);
        } else {
          Global.nextState(FrmGroupMenu.instance(), null);
        }
        Global.currentGameService = (Service) Global.gameServices.elementAt(chooseIndex);
        SocketClientUtil.connectToGameServer(Global.currentGameService.getUrl(), Global.currentGameService.getPort(), Global.gameActionHandle);
      }
      break;
    case Key.SOFT_RIGHT:
      GameGlobal.nextState(FrmGroupMenu.instance(), null);
      break;
    }
  }

  public void destroy() {
    text8 = null;
    muitenImage = null;
    muitenFocusImage = null;
  }
}
